{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reverse Prefix of Word"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reversePrefix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #反转单词前缀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个字符 <code>ch</code> 。找出 <code>ch</code> 第一次出现的下标 <code>i</code> ，<strong>反转 </strong><code>word</code> 中从下标 <code>0</code> 开始、直到下标 <code>i</code> 结束（含下标 <code>i</code> ）的那段字符。如果 <code>word</code> 中不存在字符 <code>ch</code> ，则无需进行任何操作。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，如果 <code>word = \"abcdefd\"</code> 且 <code>ch = \"d\"</code> ，那么你应该 <strong>反转</strong> 从下标 0 开始、直到下标 <code>3</code> 结束（含下标 <code>3</code> ）。结果字符串将会是 <code>\"<em><strong>dcba</strong></em>efd\"</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回 <strong>结果字符串</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word = \"<em><strong>abcd</strong></em>efd\", ch = \"d\"\n",
    "<strong>输出：</strong>\"<em><strong>dcba</strong></em>efd\"\n",
    "<strong>解释：</strong>\"d\" 第一次出现在下标 3 。 \n",
    "反转从下标 0 到下标 3（含下标 3）的这段字符，结果字符串是 \"dcbaefd\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word = \"<em><strong>xyxz</strong></em>xe\", ch = \"z\"\n",
    "<strong>输出：</strong>\"<em><strong>zxyx</strong></em>xe\"\n",
    "<strong>解释：</strong>\"z\" 第一次也是唯一一次出现是在下标 3 。\n",
    "反转从下标 0 到下标 3（含下标 3）的这段字符，结果字符串是 \"zxyxxe\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word = \"abcd\", ch = \"z\"\n",
    "<strong>输出：</strong>\"abcd\"\n",
    "<strong>解释：</strong>\"z\" 不存在于 word 中。\n",
    "无需执行反转操作，结果字符串是 \"abcd\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= word.length &lt;= 250</code></li>\n",
    "\t<li><code>word</code> 由小写英文字母组成</li>\n",
    "\t<li><code>ch</code> 是一个小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reverse-prefix-of-word](https://leetcode.cn/problems/reverse-prefix-of-word/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reverse-prefix-of-word](https://leetcode.cn/problems/reverse-prefix-of-word/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abcdefd\"\\n\"d\"', '\"xyxzxe\"\\n\"z\"', '\"abcd\"\\n\"z\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        # if ch not in word:\n",
    "        #     return word\n",
    "        # ans = \"\"\n",
    "        # for i in range(len(word)):\n",
    "        #     if ch == word[i]:\n",
    "        #         ans = word[:i+1][::-1] + word[i+1:]\n",
    "        #         break\n",
    "        # return ans\n",
    "\n",
    "        # 直接利用 find 函数简化代码\n",
    "        i = word.find(ch) + 1\n",
    "        return word[:i][::-1] + word[i:]\n",
    "\n",
    "        # 字符串反转 str[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        idx = word.find(ch)\n",
    "        return word if idx == -1 else word[idx::-1] + word[idx+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        for i,x in enumerate(word):\n",
    "            if x==ch:\n",
    "                word=word[i:0:-1]+word[0]+word[i+1:len(word)]\n",
    "                break\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        n = len(word)\n",
    "        idx = -1\n",
    "        for i in range(n):\n",
    "            if word[i] == ch:\n",
    "                idx = i\n",
    "                break\n",
    "        else:\n",
    "            return word\n",
    "        word = list(word)\n",
    "        i, j = 0, idx\n",
    "        while i < j:\n",
    "            tmp = word[i]\n",
    "            word[i] = word[j]\n",
    "            word[j] = tmp   \n",
    "            i += 1\n",
    "            j -= 1\n",
    "        word = ''.join(word)\n",
    "        return word\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch)\n",
    "        return word[:i + 1][::-1] + word[i + 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch:\n",
    "                list_s = word[:i + 1]\n",
    "                word = list_s[::-1] + word[i + 1:]\n",
    "                return word\n",
    "        return word\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        res = ''\n",
    "        ind = word.find(ch)\n",
    "        w = word[0:ind+1:]\n",
    "        w = w[::-1]\n",
    "        res += w\n",
    "        res += word[ind+1::]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        idx = word.index(ch)\n",
    "        l = [s for s in word]\n",
    "\n",
    "        left, right = 0, idx\n",
    "\n",
    "        while left < right:\n",
    "            l[left], l[right] = l[right], l[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return ''.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if not word or ch not in word:\n",
    "            return word\n",
    "        _index = word.index(ch)+1\n",
    "\n",
    "        return word[:_index][::-1]+word[_index:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i=word.find(ch)\n",
    "        return word[:i+1][::-1]+word[i+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        n = len(word)\n",
    "        for i, x in enumerate(word):\n",
    "            if x == ch:\n",
    "                return word[i::-1] + word[i+1:n]\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        start_index = 0\n",
    "        for i in range(len(word)):\n",
    "            if word[i]==ch:\n",
    "                start_index=i\n",
    "                break\n",
    "        \n",
    "        pre_word = list(word[:start_index+1])\n",
    "        pre_word.reverse()\n",
    "\n",
    "        return ''.join(pre_word) + word[start_index+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        return word if (x:=word.find(ch)) == -1 else word[:x+1][::-1] + word[x+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch) +1\n",
    "        print(i)\n",
    "        return word[:i][::-1]+word[i:]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        return word[0:word.find(ch)+1][::-1] + word[word.find(ch)+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            x = word.find(ch,0)\n",
    "            return word[x::-1]+word[x+1:]\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            ch_index = word.index(ch)\n",
    "            return word[:ch_index + 1][::-1] + word[ch_index + 1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        index = 0\n",
    "        for i in range(len(word)):\n",
    "            if word[i]==ch:\n",
    "                index = i\n",
    "                break\n",
    "        return word[:index+1][::-1]+word[index+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            return word[:(word.index(ch)+1)][::-1]+word[(word.index(ch)+1):]\n",
    "        else:\n",
    "            return word    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        index=list(word).index(ch)\n",
    "        s=''\n",
    "        for g in list(word[:index+1]):\n",
    "            s=g+s  \n",
    "        if index<len(word)+1:\n",
    "            return s+word[index+1:] \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:  \n",
    "            return word  \n",
    "        i = word.index(ch)  \n",
    "        reversed_substring = word[:i+1][::-1] + word[i+1:]  \n",
    "        return reversed_substring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if not ch in word:\n",
    "            return(word)\n",
    "        else:\n",
    "            a = int()\n",
    "            for i in range(len(word)):\n",
    "                if word[i]==ch:\n",
    "                    a = i\n",
    "                    break\n",
    "                else:\n",
    "                    continue\n",
    "            list1 = list(word[:a+1])\n",
    "            list2 = list(word[a+1:])\n",
    "            list1.reverse()\n",
    "            return ''.join(list1) + ''.join(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        result = list(word)\n",
    "        n = len(word)\n",
    "        index = float('inf')\n",
    "        for i in range(n):\n",
    "            if result[i] == ch:\n",
    "                index = i\n",
    "                break\n",
    "        if index == float('inf'):\n",
    "            return word\n",
    "        left , right = 0 , index\n",
    "        while left < right:\n",
    "            result[left] , result[right] = result[right] , result[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        flag = False\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch:\n",
    "                flag = True\n",
    "                break\n",
    "        return word[:i+1][::-1] + word[i+1:] if flag == True else word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        begin = word.find(ch)\n",
    "        if begin == -1:\n",
    "            return word\n",
    "        else:\n",
    "            newword = \"\"\n",
    "            newword = word[begin::-1] + word[begin+1:]\n",
    "            return newword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch) +1\n",
    "        print(i)\n",
    "        return word[:i][::-1]+word[i:]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch)+1\n",
    "        return word[:i][::-1]+word[i:]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i=word.find(ch)\n",
    "        return word[:i+1][::-1]+word[i+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            index=word.index(ch)\n",
    "            temp=word[0:index+1]\n",
    "            return temp[::-1]+word[index+1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        n=0\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch:\n",
    "                n=i\n",
    "                break;\n",
    "        if n==0:return word\n",
    "        last = word[n+1:]\n",
    "        first = word[0:n+1]\n",
    "        res = \"\"\n",
    "        for i in range(len(first)-1,-1,-1):\n",
    "            res = res+first[i]\n",
    "        \n",
    "        res = res+last\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        idx = word.index(ch)\n",
    "        l = [s for s in word]\n",
    "\n",
    "        left, right = 0, idx\n",
    "\n",
    "        while left < right:\n",
    "            l[left], l[right] = l[right], l[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return ''.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        res = word.find(ch)\n",
    "        if res ==-1:\n",
    "            return word\n",
    "        else:\n",
    "            return word[0:res+1][::-1] + word[res+1:]    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            ch_index = word.index(ch)\n",
    "            return word[:ch_index + 1][::-1] + word[ch_index + 1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        index = 0\n",
    "        while index<len(word) and word[index]!=ch:\n",
    "            index+=1\n",
    "        if index==len(word):return word\n",
    "        word = word[index::-1] + word[index+1:]\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        res = ''\n",
    "        res1 = ''\n",
    "        res2 = ''\n",
    "        flag = 0\n",
    "        i = 0\n",
    "        while i <= len(word)-1:\n",
    "            if word[i] == ch:\n",
    "                flag = i\n",
    "                break\n",
    "            else:\n",
    "                i += 1\n",
    "        res = word[0:flag+1]\n",
    "        res = res[::-1]\n",
    "        res1 = word[flag+1:]\n",
    "        res2 = res + res1\n",
    "        return res2\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        \"\"\"\n",
    "        word = list(word)\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch:\n",
    "                word[0:i+1] = list(reversed(word[0:i+1]))\n",
    "                break\n",
    "            else:\n",
    "                i += 1\n",
    "        return \"\".join(word)\n",
    "        \"\"\"\n",
    "        if not word or ch not in word:\n",
    "            return word\n",
    "        \n",
    "        idx = word.index(ch) + 1\n",
    "        return word[:idx][::-1] + word[idx:]\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        idx = word.index(ch)\n",
    "        l = [s for s in word]\n",
    "\n",
    "        left, right = 0, idx\n",
    "\n",
    "        while left < right:\n",
    "            l[left], l[right] = l[right], l[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return ''.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        \n",
    "        if ch not in word:\n",
    "            return word\n",
    "        n=len(word)\n",
    "        ans=''\n",
    "        for i in range(n):\n",
    "            if ch == word[i]: #找到所在的位置\n",
    "                #将前0-i个进行转置\n",
    "                ans=word[:i+1]\n",
    "                return ans[::-1]+word[i+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch:\n",
    "                return word[:i + 1][::-1] + word[i + 1:]\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = 0\n",
    "        word_list = list(word)\n",
    "        l = []\n",
    "        while i < len(word_list):\n",
    "            if word_list[i] == ch:\n",
    "                l.append(ch)\n",
    "                break\n",
    "            l.append(word_list[i])\n",
    "            i += 1\n",
    "        if i < len(word_list):\n",
    "            l = l[::-1]\n",
    "            result_list = l + word_list[i+1:]\n",
    "            return ''.join(result_list)\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\r\n",
    "        # 投机取巧法\r\n",
    "        # for index, item in enumerate(word):\r\n",
    "        #     if item == ch:\r\n",
    "        #         return word[0: index+1][::-1] + word[index+1:]\r\n",
    "        # return word\r\n",
    "        if ch not in word:\r\n",
    "            return word\r\n",
    "\r\n",
    "        left = 0\r\n",
    "        right = 0\r\n",
    "        length = len(word)\r\n",
    "        words = [item for item in word]\r\n",
    "\r\n",
    "        while left < length:\r\n",
    "            if words[left] == ch:\r\n",
    "                i = 0\r\n",
    "                while i < right + 1:\r\n",
    "                    words[i], words[right] = words[right], words[i]\r\n",
    "                    i += 1\r\n",
    "                    right -= 1\r\n",
    "                break\r\n",
    "            elif right < length:\r\n",
    "                right = left + 1\r\n",
    "\r\n",
    "            left += 1\r\n",
    "\r\n",
    "        return ''.join(words)\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if word.count(ch) == 0:\n",
    "            return word\n",
    "        index = list(word).index(ch)\n",
    "        t = word[:index+1]\n",
    "        return t[::-1] + word[index+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            num = word.index(ch)\n",
    "            return word[:num+1][::-1]+word[num+1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        mid=''\n",
    "        res=word\n",
    "        for i in range(len(word)):\n",
    "            if word[i]==ch:\n",
    "                mid=word[0:i+1]\n",
    "                mid=mid[::-1]\n",
    "                res=mid+word[i+1:]\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        left=0\n",
    "        right=0\n",
    "        for i in range(0,len(word)):\n",
    "            if word[i]==ch:\n",
    "                right=i\n",
    "                break\n",
    "        word=list(word)\n",
    "        while left <right:\n",
    "            word[left],word[right]=word[right],word[left]\n",
    "            left+=1\n",
    "            right-=1\n",
    "        word=\"\".join(word)\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        inx = word.find(ch) + 1\n",
    "        return word[:inx][::-1] + word[inx:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        idx = word.index(ch)\n",
    "        revers_word = word[:idx+1]\n",
    "        return revers_word[::-1]+word[idx+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        \"\"\"\n",
    "        tmp = \"\"\n",
    "        res = \"\"\n",
    "        not_found = True\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch and not_found:\n",
    "                tmp = tmp + word[i]\n",
    "                tmp = tmp[::-1]\n",
    "                res += tmp\n",
    "                tmp = \"\"\n",
    "                not_found = False\n",
    "            else:\n",
    "                tmp += word[i]\n",
    "\n",
    "        return res+tmp   \n",
    "        \"\"\"\n",
    "        ls = word.split(ch,1)\n",
    "        if len(ls) == 1:\n",
    "            return word\n",
    "        else:\n",
    "            return ch+ls[0][::-1]+ls[1]        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\r\n",
    "        # 投机取巧法\r\n",
    "        # for index, item in enumerate(word):\r\n",
    "        #     if item == ch:\r\n",
    "        #         return word[0: index+1][::-1] + word[index+1:]\r\n",
    "        # return word\r\n",
    "        if ch not in word:\r\n",
    "            return word\r\n",
    "\r\n",
    "        left = 0\r\n",
    "        right = 0\r\n",
    "        length = len(word)\r\n",
    "        words = [item for item in word]\r\n",
    "\r\n",
    "        while left < length:\r\n",
    "            if words[left] == ch:\r\n",
    "                i = 0\r\n",
    "                while i < right + 1:\r\n",
    "                    words[i], words[right] = words[right], words[i]\r\n",
    "                    i += 1\r\n",
    "                    right -= 1\r\n",
    "                break\r\n",
    "            elif right < length:\r\n",
    "                right = left + 1\r\n",
    "\r\n",
    "            left += 1\r\n",
    "\r\n",
    "        return ''.join(words)\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        idx = word.find(ch)\n",
    "        if idx == -1:\n",
    "            return word\n",
    "        return word[idx::-1] + word[idx + 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        tmp = \"\"\n",
    "        res = \"\"\n",
    "        not_found = True\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == ch and not_found:\n",
    "                tmp = tmp + word[i]\n",
    "                tmp = tmp[::-1]\n",
    "                res += tmp\n",
    "                tmp = \"\"\n",
    "                not_found = False\n",
    "            else:\n",
    "                tmp += word[i]\n",
    "\n",
    "        return res+tmp            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch not in word:\n",
    "            return word\n",
    "        searched = []\n",
    "        index = 0\n",
    "        for index, char in enumerate(word):\n",
    "            searched.append(char)\n",
    "            if char == ch:\n",
    "                break\n",
    "        prefix = \"\"\n",
    "        while searched:\n",
    "            prefix += searched.pop()\n",
    "        return prefix + word[index + 1 :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        left=0\n",
    "        right=0\n",
    "        for i in range(0,len(word)):\n",
    "            if word[i]==ch:\n",
    "                right=i\n",
    "                break\n",
    "        word=list(word)\n",
    "        while left <right:\n",
    "            word[left],word[right]=word[right],word[left]\n",
    "            left+=1\n",
    "            right-=1\n",
    "        word=\"\".join(word)\n",
    "        return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            list_word=list(word)\n",
    "            place_of_c=word.index(ch)\n",
    "            return word[place_of_c::-1]+word[place_of_c+1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch)\n",
    "        if 0 <= i:\n",
    "            return word[ :i+1][::-1] + word[i+1: ]\n",
    "        return word\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = -1\n",
    "        for c in range(len(word)):\n",
    "            if word[c] == ch:\n",
    "                i = c\n",
    "                break\n",
    "        return word[:i + 1][::-1] + word[i + 1: ] if i != -1 else word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch)\n",
    "        return word[:i + 1][::-1] + word[i + 1:]\n",
    "\n",
    "        # hasCH = False\n",
    "        # temp = \"\"\n",
    "        # for i, s  in enumerate(word):\n",
    "        #     temp = s + temp\n",
    "\n",
    "        #     if s == ch:\n",
    "        #         hasCH = True\n",
    "        #         if i < len(word)-1:\n",
    "        #             temp += word[i+1: len(word)]\n",
    "        #         break\n",
    "        \n",
    "        # return  temp if hasCH else word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\r\n",
    "        if ch not in word:\r\n",
    "            return word\r\n",
    "        idx = word.index(ch)\r\n",
    "        return ''.join(reversed(word[:idx + 1])) + word[idx + 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        p = word.find(ch)\n",
    "        if -1 == p:\n",
    "            return word\n",
    "        else:\n",
    "            return word[:p + 1][::-1] + word[p+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "       indx=word.find(ch)\n",
    "       return  word[indx::-1]+word[indx+1::] if indx != -1 else word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch)\n",
    "        return word[:i+1][::-1]+word[i+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            index = word.index(ch)\n",
    "            return word[index::-1] + word[index + 1::]\n",
    "        else:\n",
    "            return word\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        if ch in word:\n",
    "            return word[:word.index(ch)+1][::-1]+word[word.index(ch)+1:]\n",
    "        else:\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        i = word.find(ch) + 1\n",
    "        return word[:i][::-1] + word[i:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reversePrefix(self, word: str, ch: str) -> str:\n",
    "        index = word.find(ch)\n",
    "        if index!=-1:\n",
    "            return word[index::-1]+word[index+1:]\n",
    "        return word"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
